using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;
using net_work.Data.Entity.Hmw;
using net_work.Data.Enum.Hmw;

namespace net_work.Config.DataBaseConfig.Hmw;

/// <summary>
/// 题库题目实体配置类
/// </summary>
public class BankQuestionEntityTypeConfiguration : IEntityTypeConfiguration<BankQuestion>
{
    public void Configure(EntityTypeBuilder<BankQuestion> builder)
    {
        builder.ToTable("hmw_bank_ques");

        builder.HasKey(bq => bq.Id);

        // 配置字段名映射
        builder.Property(bq => bq.Id).HasColumnName("bank_ques_id");
        builder.Property(bq => bq.Title).HasColumnName("bank_ques_title").IsRequired().HasMaxLength(64);
        builder.Property(bq => bq.Content).HasColumnName("bank_ques_cnt").IsRequired().HasMaxLength(1024);

        // 将枚举转换为字符串存储
        builder.Property(bq => bq.Type)
            .HasColumnName("bank_ques_type")
            .IsRequired()
            .HasMaxLength(64)
            .HasConversion(
                v => v.ToString(), // 枚举转字符串
                v => (QuestionType)Enum.Parse(typeof(QuestionType), v) // 字符串转枚举
            );

        builder.Property(bq => bq.Score).HasColumnName("bank_ques_score").IsRequired();
        builder.Property(bq => bq.CourseId).HasColumnName("cou_id").IsRequired();

        builder.Property(bq => bq.CreatedBy).HasColumnName("create_by").IsRequired().HasMaxLength(64);
        builder.Property(bq => bq.CreatedTime).HasColumnName("create_time").IsRequired();
        builder.Property(bq => bq.UpdatedBy).HasColumnName("update_by").IsRequired().HasMaxLength(64);
        builder.Property(bq => bq.UpdatedTime).HasColumnName("update_time").IsRequired();
        builder.Property(bq => bq.Remark).HasColumnName("remark").HasMaxLength(256);
        builder.Property(bq => bq.DelFlag).HasColumnName("del_flag").HasDefaultValue('0');
    }
}